package br.com.tcf.arqConfiguracoes;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

public class buildLog implements Serializable {
	private FileWriter fw;
	private PrintWriter pw;
	GregorianCalendar c1;
	SimpleDateFormat sdfData;
	SimpleDateFormat sdf;
	CheckOsSystem check = new CheckOsSystem();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void acessLog(String message){
		try {
			fw = new FileWriter(caminho("acess")+dateFormat(), true);
			pw = new PrintWriter(fw);
			pw.print(message);
			pw.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public void systemLog(String message){
		try{
			fw = new FileWriter(caminho("system")+dateFormat(), true);
			pw = new PrintWriter(fw);
			pw.print(message);
			pw.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String hourFormat(){
		c1 = new GregorianCalendar();
		sdfData = new SimpleDateFormat("dd.MM.yyyy");  
		sdf = new SimpleDateFormat("HH:mm:ss");  
		String hora;
		hora=(sdfData.format(c1.getTime())+" - "+sdf.format(c1.getTime()));
		return hora;
	}
	public String dateFormat(){
		c1 = new GregorianCalendar();
		sdfData = new SimpleDateFormat("dd.MM.yyyy");  
		String hora;
		hora=(sdfData.format(c1.getTime()));
		return hora;
	}

	public static void enviaMessagemParaTela(Severity severity, String titulo, String mensagem) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(severity, titulo, mensagem));

	}
	public void atualizationLog(String message){
		try{
			fw = new FileWriter(caminho("atu")+dateFormat(), true);
			pw = new PrintWriter(fw);
			pw.print(message);
			pw.close();
			fw.close();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void emailLog(String message){
		try{
			fw = new FileWriter(caminho("email")+dateFormat(), true);
			pw = new PrintWriter(fw);
			pw.print(message);
			pw.close();
			fw.close();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String caminho(String tipo){
		String os =check.check(); 
		String caminho=null;
		if(tipo.equals("email")){
			if(os.equals("Linux")){
				return "/tcf/Logs/Email/";
			}else{
				return "C:\\tcf\\Logs\\Email\\";
			}
		}
		else if(tipo.equals("acess")){
			if(os.equals("Linux")){
				return "/tcf/Logs/Acess/";
			}else{
				return"C:\\tcf\\Logs\\Acess\\";
			}
		}
		else if(tipo.equals("system")){
			if(os.equals("Linux")){
				return "/tcf/Logs/System/";
			}else{
				return "C:\\tcf\\Logs\\System\\";
			}
		}
		return caminho;
	}

}
